package com.ikangtai.shecare.common.db.sync;

import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncNickNameMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.OKHttpUtil;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.io.IOException;

public class SyncNickName {
    private Context context;
    private SyncNickNameMsg syncNickNameMsg;

    public SyncNickName(Context context, SyncNickNameMsg syncNickNameMsg) {
        this.context = context;
        this.syncNickNameMsg = syncNickNameMsg;
        EventBus.getDefault().register(this);
    }

    public void syncNickNameWithNetwork(){
        final DBManager dbManager = new DBManager(context);

        String nickName = dbManager.getUnSyncedNickName(App.userName);

        if (nickName == null){
            EventBus.getDefault().post(syncNickNameMsg);
        } else {
            try {
                OKHttpUtil okHttpClient = new OKHttpUtil(context);
                String appendUrl = "user/modifyNickName.json";

                AppLog.i("syncTaskItemWithNetwork is syncing ...!");

                okHttpClient.add("authToken", App.authToken);
                okHttpClient.add("nickName", nickName);

                okHttpClient.postAsync(appendUrl, okHttpClient.new BaseCallBack() {

                    @Override
                    public void onFailure(Request request, IOException e) {
                        super.onFailure(request, e);
                        EventBus.getDefault().post(syncNickNameMsg);
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        int respCode = 0;

                        try {
                            JSONObject respJSONObj = new JSONObject(response.body().string());
                            respCode = respJSONObj.getInt("code");

                            if (App.HTTP_RESP_OK == respCode){
                                dbManager.updateSyncNickNameFlag(App.userName);
                                AppLog.i("syncNickNameWithNetwork onResponse success!");
                            } else if (respCode == App.HTTP_RESP_TOKEN_TIME_OUT) {
                                OKHttpUtil.setNewToken();
                                syncNickNameWithNetwork();
                            } else {
                                AppLog.i("syncNickNameWithNetwork onResponse failed! respCode is:" + respCode);
                            }
                        } catch (JSONException e) {
                            AppLog.i("syncNickNameWithNetwork JSONException!");
                            e.printStackTrace();
                        }

                        if (respCode != App.HTTP_RESP_TOKEN_TIME_OUT)
                            EventBus.getDefault().post(syncNickNameMsg);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
                EventBus.getDefault().post(syncNickNameMsg);
            }
        }
    }
}
